package m202406.d20;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/20 21:17
 */
public class Q82 {

    public static void main(String[] args) {
        Q82 q82 = new Q82();
        ListNode l = new ListNode(1);
        l.next = new ListNode(1);
        q82.deleteDuplicates(l);
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode newHead = new ListNode(-1, head);

        ListNode p = newHead;
        ListNode p1 = newHead.next;
        ListNode p2 = newHead.next.next;

        while (p2 != null) {
            if (p2.val != p1.val) {
                p = p.next;
                p1 = p1.next;
                p2 = p2.next;
            } else {
                while (p2 != null && p1.val == p2.val) {
                    p2 = p2.next;
                }
                p.next = p2;
                p1 = p2;
                p2 = p2 == null ? null : p2.next;
            }
        }
        return newHead.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
